<?php
/**
 * Created by JetBrains PhpStorm.
 * File: Component.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-15
 * Time: 下午6:45
 */

class ComponentAction extends CommonAction{

	//组件列表
	function index(){

		$component = D('Component');
		import("ORG.Util.page");

		if(isset($_POST['search'])){
			if(empty($_POST['key'])){
				$this->error('不能为空');
			}else{
				$datas = array();
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
				}
				$list = $component->where($datas)->order('id desc')->select();
				$count = $component->where($datas)->count('id');
				if($count<1){
					//	$this->assign('isshow', 'no');
					$this->error('没有该组件数据');
				}elseif($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));

				$show = $page->fpage($pagedata);
				$this->assign('isshow', 'yes');
				$this->assign('nobody', 'yes');

			}

		}else{
			$count = $component->count();
			if($count<C('PAGENUM')){
				$pagedata = array(1);
			}else{
				$pagedata = array(0,1,2,3,4,5,7,8);
			}
			$page = new page($count, C('PAGENUM'));
			$show = $page->fpage($pagedata);
			$list = $component->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
			//				for($i=0; $i<count($list); $i++){
			//					$list[$i]['num'] = count($list[$i]['roleuser']);
			//				}
			if($count==0){
				$this->assign('isshow', 'no');
				$this->assign('nobody', 'no');
			}else{
				$this->assign('isshow', 'yes');
				$this->assign('nobody', 'yes');
			}
		}

		$this->assign('show', $show);
		$this->assign('list', $list);
		$this->display('index');
	}

	//指向添加组件界面
	function add(){
		$this->display('add');
	}

	//添加菜单
	function insert(){
		$component = D('Component');
		if($component->create()){
			if($component->add()){
				$this->success('添加组件成功', __APP__.'/Component/index');
			}else{
				$this->error('添加组件失败');
			}
		}else{
			$this->error($component->getError());
		}
	}

	//删除组件
	function del(){
		if(!empty($_POST['del'])){
			$component = D('Component');
			foreach($_POST['del'] as $key=>$value){
				$list = $component->where('id='.$value)->select();
					$num = $component->delete($value);
				if($num>0){
					$this->success('删除组件成功');
				}else{
					$this->error('删除组件失败');
				}
			}
		}else{
			$this->error('请选择组件');
		}
	}

	//指向修改组件界面
	function edit(){
		if($_GET['id'] && !empty($_GET['id'])){
			$component = M('Component');
			$list = $component->find($_GET['id']);

			$this->assign('list', $list);
			$this->display();
		}else{
			$this->error('异常操作');
		}
	}

	//修改分组
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$component = D('Component');
			if($list = $component->create()){
				if($component->save()){
					$this->success('修改组件成功', __APP__.'/Component/index');
				}else{
					$this->error('修改组件失败');
				}
			}else{
				$this->error($component->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

}